<?php

/**
 * Class that manages products and projects
 */
class BabiliProduct{
	
	//the db connector
	private $db;

	/**
	 * The construct load the db connector
	 * @param $db
	 */
	public function __construct($db){
		$this->db = $db;
	}
	
	
	/**
	 * Function that add a product
	 * @param $name
	 * @param $label
	 * @param $version
	 * @param $status (enable/disable)
	 * @return $productid the id of the product, NULL else
	 */
	public function add($name, $label, $version, $status){
		
		if(empty($name) || empty($label) || empty($version) || empty($status)){
			return NULL;
		}
		
		//insert in database
		$query = "INSERT INTO product (name, label, version, status)
				  VALUES (:name, :label, :version, :status)";
		$query_args = array(':name' => $name,
							':label' => $label,
							':version' => $version,
							':status' => $status);
		
		if($this->db->update($query, $query_args)){
			//return the id
			return $this->db->last_id();			 
		}
		
		
	}
	
	/**
	 * Function that modify a product
	 * @param $id
	 * @param $name
	 * @param $label
	 * @param $version
	 * @param $status (enable/disable)
	 * @return bool true if successful, false else
	 */
	public function modify($id, $name, $label, $version, $status){
		
		if(!is_numeric($id) || empty($name) || empty($label) || empty($version) || empty($status)){
			return false;
		}
		
		$query = "UPDATE product SET ";
		$query_args = array();
	
		$query .= "label = :label, name = :name, version = :version, status = :status " ;
		$query_args[':label'] = $label;
		$query_args[':name'] = $name;
		$query_args[':version'] = $version;
		$query_args[':status'] = $status;
		
		$query .= "WHERE id = :id";
		$query_args[':id'] = $id;
		
		return $this->db->update($query, $query_args);
	
	}
	
	/**
	 * Function that give product informations
	 * @param $id
	 * @param $project_status ('all' => we want all projects of the products
	 * 						   'enable' => we want all projects enable [DEFAULT]
	 * 						   'disable' => we want all projects disable)
	 * @return $product = array('id' => $id,
	 *			 				 'name' => $name,
	 *			 				 'label' => $label,
	 *			 				 'version' => $version,
	 *			 				 'status' => $status,
	 *			 				 'projects' => array($projectid => array('id' => $id, 'name' => $name, 'label' => $label, 'version' => $version, 'status' => $status),
	 *			 									 $projectid2 => array('id' => $id, 'name' => $name, 'label' => $label, 'version' => $version, 'status' => $status),
	 *			 									...))
	 */
	public function get($id,$project_status = 'enable'){
		
		if (!is_numeric($id) || empty($project_status)){
			return NULL;
		}
		
		// select all informations of the product
		
		$query = "SELECT * FROM product WHERE id = :id_product";		
		$query_args = array(':id_product' => $id);
		
		$product = reset($this->db->select($query, $query_args));
		
		//get all the projects
		$projects = $this->get_all_projects($id, $project_status);
		
		$product['projects'] = $projects;
		return $product;		
	}
	
	/**
	 * Function that gives all products
	 * @param $status ('all' => we want all products
	 * 						   'enable' => we want all products enable [DEFAULT]
	 * 						   'disable' => we want all products disable)
	 * @return array of $product (see the function get)
	 */
	public function get_all($status = 'enable'){
		$query = "SELECT id 
				  FROM product WHERE status = :status";
		
		$query_args = array(':status' => $status);
		
		$result = $this->db->select($query, $query_args);
		
		$productids = array();
		foreach ($result as $key => $value){
			$productids[$value["id"]] = $this->get($value["id"], $status);
		}
		return $productids;
		
	}
	
	/**
	 * Function that add languages to a product
	 * @param $product_id
	 * @param $languages array of language ids (1,2,3)
	 * @return bool true if successful, false else
	 */
	public function add_languages($product_id, $languages){
		if (!is_numeric($product_id) || empty($languages)){
			return false;
		}
		
		foreach($languages as $key => $language_id){
			//insert in database
			$query = "INSERT INTO product_preferences (product_id, language_id)
					  VALUES (:product_id, :language_id)";
			$query_args = array(':product_id' => $product_id,
								':language_id' => $language_id);
			
			$this->db->update($query, $query_args);
		}
		
		return true;
	}
	
	/**
	 * Function that add languages to a product
	 * @param $product_id
	 * @param $languages array of language ids (1,2,3)
	 * @return bool true if successful, false else
	 */
	public function remove_languages($product_id, $languages){
		if (!is_numeric($product_id) || empty($languages)){
			return false;
		}
		
		foreach($languages as $key => $language_id){
			//delete from database
			$query = "DELETE FROM product_preferences WHERE product_id = :product_id AND language_id = :language_id";
					
			$query_args = array(':product_id' => $product_id,
								':language_id' => $language_id);
			
			$this->db->update($query, $query_args);
		}
		
		return true;
		
	}
	
	/**
	 * Function that add a project
	 * @param $name
	 * @param $label
	 * @param $version
	 * @param $status (enable/disable)
	 * @param $productids array of product id (a project belong to one product minimum)
	 * @return the id of the project added, NULL if wrong
	 */
	public function add_project($name, $label, $version, $status, $productids){

		if(empty($name) || empty($label) || empty($version) || empty($status) || empty($productids)){
			return NULL;
		}
		
		//insert in database
		$query = "INSERT INTO project (name, label, version, status)
				  VALUES (:name, :label, :version, :status)";
		$query_args = array(':name' => $name,
							':label' => $label,
							':version' => $version,
							':status' => $status);
		
		$this->db->update($query, $query_args);

		// id of the created project in the method
		$project_id = $this->db->last_id();
		
		foreach($productids as $key => $product_id){
			//insert in database
			$query = "INSERT INTO product_project (product_id, project_id)
					  VALUES (:product_id, :project_id)";
			$query_args = array(':product_id' => $product_id,
								':project_id' => $project_id);
			
			$this->db->update($query, $query_args);
		}
		
		return $project_id;
		
	 
	}
	
	
	/*
	 * Function that modify a project
	 * @param $id
	 * @param $name
	 * @param $label
	 * @param $version
	 * @param $status (enable/disable)
	 * @param $productids array of product id (a project belong to one product minimum)
	 */
	public function modify_project($id, $name, $label, $version, $status, $productids){

		if(!is_numeric($id) || empty($name) || empty($label) || empty($version) || empty($status)){
			return false;
		}
		
		// modify the project
		
		$query = "UPDATE project SET ";
		$query_args = array();
	
		$query .= "label = :label, name = :name, version = :version, status = :status " ;
		$query_args[':label'] = $label;
		$query_args[':name'] = $name;
		$query_args[':version'] = $version;
		$query_args[':status'] = $status;
		
		$query .= "WHERE id = :id";
		$query_args[':id'] = $id;
		
		$this->db->update($query, $query_args);
		
		// flush the table "product_project"..
		
		$query = "DELETE FROM product_project WHERE project_id = :project_id";
				
		$query_args = array(':project_id' => $id);
		
		$this->db->update($query, $query_args);
		
		// .. and insert all the new values

		foreach($productids as $key => $product_id){

			$query = "INSERT INTO product_project (product_id, project_id)
					  VALUES (:product_id, :project_id)";
			$query_args = array(':product_id' => $product_id,
								':project_id' => $id);
			
			$this->db->update($query, $query_args);
		}
		
		return true;
	}
	
	/*
	 * Function that gives informations about a project
	 * @param $id
	 * @return array('id' => $id,
	 * 				 'name' => $name,
	 * 				 'label' => $label,
	 * 				 'version' => $version,
	 * 				 'status' => $status
	 */
	public function get_project($id){
		
		if (!is_numeric($id)){
			return NULL;
		}		
		
		$query = "SELECT * from project WHERE id = :id_project";
		$query_args = array(':id_project' => $id);
		
		return reset($this->db->select($query, $query_args));
		
	}
	
	/*
	 * Function that gives all projects of a product
	 * @param $productid
	 * @param $project_status ('all' => we want all projects of the products
	 * 						   'enable' => we want all projects enable [DEFAULT]
	 * 						   'disable' => we want all projects disable)
	 * @return array($projectid => array('id' => $id, 'name' => $name, 'label' => $label, 'version' => $version, 'status' => $status),
	 * 				$projectid2 => array('id' => $id, 'name' => $name, 'label' => $label, 'version' => $version, 'status' => $status),
	 * 				...
	 * 				)
	 */
	public function get_all_projects($productid, $project_status = 'enable'){

		// select all ids of projects 
		$query = "SELECT id, name, label, version, status
				  FROM project as proj
				  INNER JOIN product_project as prodproj ON proj.id = prodproj.project_id
				  WHERE prodproj.product_id = :product_id";
		
		
		if ($project_status == "enable" || $project_status == "disable"){ 
				$query .= " AND proj.status = :project_status";
				$query_args = array(':product_id' => $productid,
									':project_status' => $project_status);
		}else{
			$query_args = array(':product_id' => $productid);
		}
		
		
		
		$results = $this->db->select($query, $query_args);
		
		//init $projects, array with ou results
		$projects = array();
		//set the index of the array on project_id
		foreach ($results as $result){
			$projects[$result[id]] = $result;
			
		}
		
		return $projects;
			
	}
}